﻿using System;
using Entities;
using Business;
namespace ServicesDirectory.UserControl
{
    public partial class OrganisationDetail : System.Web.UI.UserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            string orgId = Request.QueryString["orgId"];
            if (!IsPostBack)
                if (orgId == null)
                {
                    ViewState["ModeForm"] = "Add";
                    lbtDetail3.Visible = false;
                    lbtDetail4.Visible = false;
                    lbtDetail5.Visible = false;
                    lbtDetail6.Visible = false;
                    pnDetail1.Visible = true;
                    pnDetail2.Visible = false;
                    pnDetail3.Visible = false;
                    pnDetail4.Visible = false;
                    pnDetail5.Visible = false;
                    pnDetail6.Visible = false;
                    btnInactive.Visible = false;
                    
                }
                else
                {
                    if (Request.QueryString["isActive"] != null)
                        OrganizationBusiness.Instance.MarkInActive(orgId);
                    ViewState["orgId"] = orgId;
                    Session["orgId"] = orgId;
                    ViewState["ModeForm"] = "Edit";
                    //bind Data
                    BindData(orgId);
                    OrganisationDetail2.SetData += OrganisationDetail2_SetData;
                    OrganisationDetail3.SetDataDetail3 += OrganisationDetail3_SetDataDetail3;
                    OrganisationDetail5.SetDataDetail5 += OrganisationDetail5_SetDataDetail5;
                    ListDirectorate1.SetDataDetail6 += ListDirectorate1_SetDataDetail6;
                    lbtDetail1.Visible = true;
                    lbtDetail2.Visible = true;
                    lbtDetail4.Visible = true;
                    lbtDetail5.Visible = true;
                    lbtDetail6.Visible = true;
                    pnDetail1.Visible = true;
                    pnDetail2.Visible = false;
                    pnDetail3.Visible = false;
                    pnDetail4.Visible = false;
                    pnDetail5.Visible = false;
                    pnDetail6.Visible = false;
                    btnInactive.Visible = true;

                }

            lbtDetail1.Click += LbtDetail1_Click;
            lbtDetail2.Click += LbtDetail2_Click;
            lbtDetail3.Click += LbtDetail3_Click;
            lbtDetail4.Click += LbtDetail4_Click;
            lbtDetail5.Click += LbtDetail5_Click;
            lbtDetail6.Click += LbtDetail6_Click;
            OrganisationDetail1.ExpressionCheckedChange += OrganisationDetail1_ExpressionCheckedChange;
            cmdBack.Click += cmdBack_Click;
            btnInactive.Click += BtnInactive_Click;
        }

        void ListDirectorate1_SetDataDetail6(object sender, EventArgs e)
        {
            if (ViewState["orgId"] != null)
            {
                ListDirectorate1.Display(new Guid(ViewState["orgId"].ToString()));
            }
        }

        void BtnInactive_Click(object sender, EventArgs e)
        {
            if (ViewState["orgId"] != null)
            {
                var org = new Organisation();
                org.OrgId = new Guid(ViewState["orgId"].ToString());
                OrganisationDetail1.GetData();
                org.AddressLine1 = OrganisationDetail1.AddressLine1;
                org.AddressLine2 = OrganisationDetail1.AddressLine2;
                org.AddressLine3 = OrganisationDetail1.AddressLine3;
                org.BusinessId = OrganisationDetail1.BusinessId;
                org.CharityNumber = OrganisationDetail1.CharityNumber;
                org.CompanyNumber = OrganisationDetail1.CompanyNumber;
                org.ContactId = OrganisationDetail1.ContactId;
                org.Email = OrganisationDetail1.Email;
                org.ExpressionOfBusiness = OrganisationDetail1.ExpressionOfBusiness;
                org.Fax = OrganisationDetail1.Fax;
                org.OrgFullDescription = OrganisationDetail1.OrgFullDescription;
                org.OrgName = OrganisationDetail1.OrgName;
                org.OrgPreferred = OrganisationDetail1.OrgPreferred;
                org.PhoneNumber = OrganisationDetail1.PhoneNumber;
                org.ShortDescription = OrganisationDetail1.ShortDescription;
                org.WebAddress = OrganisationDetail1.WebAddress;
                org.AddressId = OrganisationDetail1.AddressId;

                OrganisationDetail2.GetData();

                org.Accreditation = OrganisationDetail2.Accreditation;
                org.ServiceBarriers = OrganisationDetail2.ServiceBarriers;
                org.ServiceBenefits = OrganisationDetail2.ServiceBenefits;
                org.ServiceDisability = OrganisationDetail2.ServiceDisability;
                org.ServiceEthnicity = OrganisationDetail2.ServiceEthnicity;
                org.ServicePersonalCircumstances = OrganisationDetail2.ServicePersonalCircumstances;
                org.Specialism = OrganisationDetail2.Specialism;


                OrganisationDetail3.GetData();
                org.EoiProgrammes = OrganisationDetail3.EoiProgrammes;
                org.EoiService = OrganisationDetail3.EoiService;
                org.IsActive = false;
                if (OrganizationBusiness.Instance.UpdateOrganisation(org))
                {
                    lblMessage.Text = "Update organisation successfull";
                    lblMessage.Visible = true;
                }
                else
                {
                    lblMessage.Text = "Update fail";
                    lblMessage.Visible = true;
                }
            }
        }

        void OrganisationDetail5_SetDataDetail5(object sender, EventArgs e)
        {
            if (ViewState["orgId"] != null)
            {
                var organisation = OrganizationBusiness.Instance.GetOrganisationById(ViewState["orgId"].ToString());
                if (organisation != null)
                {
                    OrganisationDetail5.BindData(organisation.OrgId.ToString(), true);
                }
            }
        }

        void OrganisationDetail3_SetDataDetail3(object sender, EventArgs e)
        {
            //bind organisation detail3
            if (ViewState["orgId"] != null)
            {
                var organisation = OrganizationBusiness.Instance.GetOrganisationById(ViewState["orgId"].ToString());
                if (organisation != null)
                {
                    OrganisationDetail3.EoiProgrammes = organisation.EoiProgrammes;
                    OrganisationDetail3.EoiService = organisation.EoiService;
                    OrganisationDetail3.LoadData();
                }
            }
        }

        void OrganisationDetail2_SetData(object sender, EventArgs e)
        {
            if (ViewState["orgId"] != null)
            {
                var organisation = OrganizationBusiness.Instance.GetOrganisationById(ViewState["orgId"].ToString());
                if (organisation != null)
                {
                    OrganisationDetail2.Accreditation = organisation.Accreditation;
                    OrganisationDetail2.Specialism = organisation.Specialism;
                    OrganisationDetail2.ServiceBarriers = organisation.ServiceBarriers;
                    OrganisationDetail2.ServiceBenefits = organisation.ServiceBenefits;
                    OrganisationDetail2.ServiceDisability = organisation.ServiceDisability;
                    OrganisationDetail2.ServiceEthnicity = organisation.ServiceEthnicity;
                    OrganisationDetail2.ServicePersonalCircumstances = organisation.ServicePersonalCircumstances;
                    OrganisationDetail2.LoadData();
                }
            }
        }

        void cmdBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("ListOrganisation.aspx", true);
        }

        void OrganisationDetail1_ExpressionCheckedChange(object sender, EventArgs e)
        {
            if ((bool)sender)
            {
                lbtDetail3.Visible = true;
            }
            else
            {
                lbtDetail3.Visible = false;
            }
        }

        void LbtDetail6_Click(object sender, EventArgs e)
        {
            lbtDetail1.CssClass = "";
            lbtDetail2.CssClass = "";
            lbtDetail3.CssClass = "";
            lbtDetail4.CssClass = "";
            lbtDetail5.CssClass = "";
            lbtDetail6.CssClass = "active";

            pnDetail1.Visible = false;
            pnDetail2.Visible = false;
            pnDetail3.Visible = false;
            pnDetail4.Visible = false;
            pnDetail5.Visible = false;
            pnDetail6.Visible = true;

        }

        void LbtDetail5_Click(object sender, EventArgs e)
        {
            lbtDetail1.CssClass = "";
            lbtDetail2.CssClass = "";
            lbtDetail3.CssClass = "";
            lbtDetail4.CssClass = "";
            lbtDetail5.CssClass = "active";
            lbtDetail6.CssClass = "";


            pnDetail1.Visible = false;
            pnDetail2.Visible = false;
            pnDetail3.Visible = false;
            pnDetail4.Visible = false;
            pnDetail5.Visible = true;
            pnDetail6.Visible = false;
        }

        void LbtDetail4_Click(object sender, EventArgs e)
        {
            lbtDetail1.CssClass = "";
            lbtDetail2.CssClass = "";
            lbtDetail3.CssClass = "";
            lbtDetail4.CssClass = "active";
            lbtDetail5.CssClass = "";
            lbtDetail6.CssClass = "";


            pnDetail1.Visible = false;
            pnDetail2.Visible = false;
            pnDetail3.Visible = false;
            pnDetail4.Visible = true;
            pnDetail5.Visible = false;
            pnDetail6.Visible = false;
        }

        void LbtDetail3_Click(object sender, EventArgs e)
        {

            lbtDetail1.CssClass = "";
            lbtDetail2.CssClass = "";
            lbtDetail3.CssClass = "active";
            lbtDetail4.CssClass = "";
            lbtDetail5.CssClass = "";
            lbtDetail6.CssClass = "";


            pnDetail1.Visible = false;
            pnDetail2.Visible = false;
            pnDetail3.Visible = true;
            pnDetail4.Visible = false;
            pnDetail5.Visible = false;
            pnDetail6.Visible = false;
        }

        void LbtDetail2_Click(object sender, EventArgs e)
        {

            lbtDetail1.CssClass = "";
            lbtDetail2.CssClass = "active";
            lbtDetail3.CssClass = "";
            lbtDetail4.CssClass = "";
            lbtDetail5.CssClass = "";
            lbtDetail6.CssClass = "";


            pnDetail1.Visible = false;
            pnDetail2.Visible = true;
            pnDetail3.Visible = false;
            pnDetail4.Visible = false;
            pnDetail5.Visible = false;
            pnDetail6.Visible = false;
        }

        void LbtDetail1_Click(object sender, EventArgs e)
        {

            lbtDetail1.CssClass = "active";
            lbtDetail2.CssClass = "";
            lbtDetail3.CssClass = "";
            lbtDetail4.CssClass = "";
            lbtDetail5.CssClass = "";
            lbtDetail6.CssClass = "";


            pnDetail1.Visible = true;
            pnDetail2.Visible = false;
            pnDetail3.Visible = false;
            pnDetail4.Visible = false;
            pnDetail5.Visible = false;
            pnDetail6.Visible = false;
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(ViewState["ModeForm"].ToString()) || ViewState["ModeForm"].ToString().Equals("Add"))
            {
                var org = new Organisation();
                org.OrgId = Guid.NewGuid();
                OrganisationDetail1.GetData();
                org.AddressLine1 = OrganisationDetail1.AddressLine1;
                org.AddressLine2 = OrganisationDetail1.AddressLine2;
                org.AddressLine3 = OrganisationDetail1.AddressLine3;
                org.BusinessId = OrganisationDetail1.BusinessId;
                org.CharityNumber = OrganisationDetail1.CharityNumber;
                org.CompanyNumber = OrganisationDetail1.CompanyNumber;
                org.ContactId = OrganisationDetail1.ContactId;
                org.Email = OrganisationDetail1.Email;
                org.ExpressionOfBusiness = OrganisationDetail1.ExpressionOfBusiness;
                org.Fax = OrganisationDetail1.Fax;
                org.OrgFullDescription = OrganisationDetail1.OrgFullDescription;
                org.OrgName = OrganisationDetail1.OrgName;
                org.OrgPreferred = OrganisationDetail1.OrgPreferred;
                org.PhoneNumber = OrganisationDetail1.PhoneNumber;
                org.ShortDescription = OrganisationDetail1.ShortDescription;
                org.WebAddress = OrganisationDetail1.WebAddress;
                org.AddressId = OrganisationDetail1.AddressId;

                OrganisationDetail2.GetData();

                org.Accreditation = OrganisationDetail2.Accreditation;
                org.ServiceBarriers = OrganisationDetail2.ServiceBarriers;
                org.ServiceBenefits = OrganisationDetail2.ServiceBenefits;
                org.ServiceDisability = OrganisationDetail2.ServiceDisability;
                org.ServiceEthnicity = OrganisationDetail2.ServiceEthnicity;
                org.ServicePersonalCircumstances = OrganisationDetail2.ServicePersonalCircumstances;
                org.Specialism = OrganisationDetail2.Specialism;


                OrganisationDetail3.GetData();
                org.EoiProgrammes = OrganisationDetail3.EoiProgrammes;
                org.EoiService = OrganisationDetail3.EoiService;
                org.IsActive = false;
                if (OrganizationBusiness.Instance.InsertOrganisation(org))
                {
                    lblMessage.Text = "Add new organisation successfull";
                    lblMessage.Visible = true;
                    btnInactive.Visible = true;
                    if (OrganisationDetail1.ExpressionOfBusiness)
                        lbtDetail3.Visible = true;
                    lbtDetail4.Visible = true;
                    lbtDetail5.Visible = true;
                    lbtDetail6.Visible = true;
                }
                else
                {
                    lblMessage.Text = "Insert fail";
                    lblMessage.Visible = true;
                }


            }
            else
            {
                if (ViewState["orgId"] != null)
                {
                    var org = new Organisation();
                    org.OrgId = new Guid(ViewState["orgId"].ToString());
                    OrganisationDetail1.GetData();
                    org.AddressLine1 = OrganisationDetail1.AddressLine1;
                    org.AddressLine2 = OrganisationDetail1.AddressLine2;
                    org.AddressLine3 = OrganisationDetail1.AddressLine3;
                    org.BusinessId = OrganisationDetail1.BusinessId;
                    org.CharityNumber = OrganisationDetail1.CharityNumber;
                    org.CompanyNumber = OrganisationDetail1.CompanyNumber;
                    org.ContactId = OrganisationDetail1.ContactId;
                    org.Email = OrganisationDetail1.Email;
                    org.ExpressionOfBusiness = OrganisationDetail1.ExpressionOfBusiness;
                    org.Fax = OrganisationDetail1.Fax;
                    org.OrgFullDescription = OrganisationDetail1.OrgFullDescription;
                    org.OrgName = OrganisationDetail1.OrgName;
                    org.OrgPreferred = OrganisationDetail1.OrgPreferred;
                    org.PhoneNumber = OrganisationDetail1.PhoneNumber;
                    org.ShortDescription = OrganisationDetail1.ShortDescription;
                    org.WebAddress = OrganisationDetail1.WebAddress;
                    org.AddressId = OrganisationDetail1.AddressId;

                    OrganisationDetail2.GetData();

                    org.Accreditation = OrganisationDetail2.Accreditation;
                    org.ServiceBarriers = OrganisationDetail2.ServiceBarriers;
                    org.ServiceBenefits = OrganisationDetail2.ServiceBenefits;
                    org.ServiceDisability = OrganisationDetail2.ServiceDisability;
                    org.ServiceEthnicity = OrganisationDetail2.ServiceEthnicity;
                    org.ServicePersonalCircumstances = OrganisationDetail2.ServicePersonalCircumstances;
                    org.Specialism = OrganisationDetail2.Specialism;


                    OrganisationDetail3.GetData();
                    org.EoiProgrammes = OrganisationDetail3.EoiProgrammes;
                    org.EoiService = OrganisationDetail3.EoiService;
                    org.IsActive = true;
                    if (OrganizationBusiness.Instance.UpdateOrganisation(org))
                    {
                        lblMessage.Text = "Update organisation successfull";
                        lblMessage.Visible = true;
                    }
                    else
                    {
                        lblMessage.Text = "Update fail";
                        lblMessage.Visible = true;
                    }
                }
            }
        }
        void BindData(string orgId)
        {
            var organisation = OrganizationBusiness.Instance.GetOrganisationById(orgId);
            if (organisation != null)
            {
                //bind organisation detai1
                OrganisationDetail1.AddressId = organisation.AddressId;
                OrganisationDetail1.AddressLine1 = organisation.AddressLine1;
                OrganisationDetail1.AddressLine2 = organisation.AddressLine2;
                OrganisationDetail1.AddressLine3 = organisation.AddressLine3;
                OrganisationDetail1.BusinessId = organisation.BusinessId;
                OrganisationDetail1.CharityNumber = organisation.CharityNumber;
                OrganisationDetail1.CompanyNumber = organisation.CompanyNumber;
                OrganisationDetail1.ContactId = organisation.ContactId;
                OrganisationDetail1.Email = organisation.Email;
                OrganisationDetail1.ExpressionOfBusiness = organisation.ExpressionOfBusiness;
                if (organisation.ExpressionOfBusiness)
                {
                    lbtDetail3.Visible = true;
                }
                OrganisationDetail1.Fax = organisation.Fax;
                OrganisationDetail1.OrgFullDescription = organisation.OrgFullDescription;
                OrganisationDetail1.OrgName = organisation.OrgName;
                OrganisationDetail1.OrgPreferred = organisation.OrgPreferred;
                OrganisationDetail1.WebAddress = organisation.WebAddress;
                OrganisationDetail1.ShortDescription = organisation.ShortDescription;
                OrganisationDetail1.PhoneNumber = organisation.PhoneNumber;
                OrganisationDetail1.LoadData();


                //bind organisation detail4               

            }
        }


    }

}